package com.jtcl.module.rest;

import cn.hutool.core.util.IdUtil;
import com.jtcl.config.OrderContext;
import com.jtcl.config.OrderStateMachineEngine;
import com.jtcl.enums.OrderEvent;
import com.jtcl.enums.OrderState;
import com.jtcl.module.dto.OrderDTO;
import com.jtcl.module.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sjt
 * @since 2021-03-02 18:17
 */
@RestController
@RequestMapping(value = "/order/")
public class OrderRest {

    /**
     * 注入状态机
     */
    @Autowired
    OrderStateMachineEngine orderStateMachineEngine;
    @Autowired
    OrderMapper orderMapper;

    @GetMapping("/test")
    public void test() {
        OrderDTO orderDTO = new OrderDTO(IdUtil.fastSimpleUUID(),OrderState.UNFOUND,null,null);
        OrderContext orderContext = new OrderContext(orderDTO,orderDTO.getId());
        orderStateMachineEngine.fire(OrderEvent.FOUND, orderContext);
    }

    @GetMapping("submitEvent")
    public void submitEvent(String id,String event){
        OrderDTO orderDTO = orderMapper.selectById(id);
        OrderContext orderContext = new OrderContext(orderDTO,orderDTO.getId());
        orderStateMachineEngine.fire(OrderEvent.valueOf(event), orderContext);
    }


}

